import api from "../../api/index";
// import { getUserInfo } from "../../services/user";
import config from "../../utils/config";

Component({
    properties: {
        showPhoneModal: {
            type: Boolean,
            default: false
        }
    },

    methods: {
        handlerPhoneNumber(res) {
            // console.log(res)
            if (res.detail.errMsg === 'getPhoneNumber:ok') {
                this.saveWxMobile(res)
            }
        },

        saveWxMobile(res){
            api.post(`/api/user/savewamobile/${config.orgCode}/${config.appCode}`, {
                data: {
                    encrypteddata: res.detail.encryptedData,
                    iv: res.detail.iv,
                }
            }).then(res => {
                wx.showToast({
                    title: '绑定成功',
                })

                getUserInfo().then(res => {
                    this.triggerEvent('phone-bind-success')
                })
            }).catch(e => {
                console.log(e)
                wx.showToast({
                    icon: 'none',
                    title: '绑定失败：' + e.faultstring,
                })
                this.triggerEvent('error')
            })
        },

        Close() {
            this.setData({
                showPhoneModal: false
            })
        }
    },
})